package com.zdy.worldtion.app;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.StrictMode;
import android.text.TextUtils;

import com.zdy.worldtion.common.Constant;
import com.zdy.worldtion.utils.PreUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author wxl
 */
public class MyApplication extends Application {

    public static final String APP_ID = "wxdf1b4f06ba2e7793";

    /**
     * 保存所有已创建的Activity
     */
    private static Stack<Activity> activityStack;
    // singleton
    private static MyApplication context = null;

    //App的上下文
    public static Context mContext = null;
    //屏幕宽
    public static int width;
    //屏幕高
    public static int height;
    public static String avatar = "";
    public static String token = "";
    public static String realname = "";
    public static String username = "";
    private static String scheduleId;
    private static String fromUser;
    //所有的activity列表
    private List<Activity> activities;

    public boolean isSDKInit;//SDK是否初始化
    private Handler mainHandler;

    private String hxName;

    private String owId, type;
    private boolean isAgreedPrivate;//是否同意隐私政策，true同意，false不同意

    @Override
    public void onCreate() {
        super.onCreate();

        context = this;
        mContext = this;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
        activities = new ArrayList<>();
        AppManager.initialize(this);
        try {
            isAgreedPrivate = (boolean) PreUtils.get(Constant.IS_AGREE_PRIVATE_RULE, false);
        } catch (Exception e) {
            e.printStackTrace();
            isAgreedPrivate = false;
        }
        if (isAgreedPrivate) {
            //用户同意隐私政策
        }
    }

    public void addActivities(Activity a) {
        activities.add(a);
    }

    /**
     * 判断应用是否在前台
     *
     * @param context
     * @return
     */
    public static boolean isForeground(Activity context, String className) {
        if (context == null || TextUtils.isEmpty(className))
            return false;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName()))
                return true;
        }
        return false;
    }

    /**
     * 判断应用是否在后台运行
     *
     * @param context
     * @param proessName
     * @return
     */
    public static boolean isProessRunning(Context context, String proessName) {

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> lists = am.getRunningAppProcesses();

        for (ActivityManager.RunningAppProcessInfo info : lists) {
            if (info.processName.equals(proessName)) {
                return true;
            }
        }
        return false;
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // 配置MultiDex(第五步)
        // 将MultiDex注入到项目中

    }

    public static MyApplication getInstance() {
        return context;
    }


    /**
     * Activity被销毁时，从Activities中移除
     */
    public void remove(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }
}
